<?php
/**
 * User: Allen
 * Date: 15-10-22
 * 保险版本控制器
 */

class Appoint extends PC_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->load->model('appoint_model');
	}
	
	/**
	 * @brief 保险版本信息页面
	 * @param Null
	 * @par 2015/10/22 Ver 1.00 Created by Allen
	 */	
	public function appoint_list() {	
		$this->data['order_id'] =  empty($_POST) ? '' : $_POST['order_id'];
		$this->data['appoint_status'] =  empty($_POST) ? '3' : $_POST['appoint_status'];	
		$this->load->view('/www/appoint_list.php',$this->data);
	}
	
	/**
	 * @brief ajxa获取保险版本信息
	 * @param Null
	 * @par 2015/10/22 Ver 1.00 Created by Allen
	 */
	public function ajax_appoint_list() {
		$data = $_GET;
		$start = $data['iDisplayStart']; //显示的起始索引
		$length = $data['iDisplayLength'];//显示的行数
		$sort_th = $data['mDataProp_'.$data['iSortCol_0'].''];//被排序的列 
		$sort_type = $data['sSortDir_0'];//排序的方向 "desc" 或者 "asc".
		$search = $data ['sSearch'];//全局搜索字段
		$where['concat(appoint.id) like'] = '%'.trim($search).'%';
		$data['order_id'] ? $where['appoint.order_id like'] = '%'.trim($data['order_id']).'%' :'';
		if($data['appoint_status'] == 1) {
			$where['appoint.appoint_status'] = 1;
		}else if($data['appoint_status'] == 2) {
			$where['appoint.appoint_status'] = 2;
		}
		$this->data['count'] = $this->appoint_model->getCount($where);
		$aaData = array();
		if($this->data['count']) {
			$order_by = $sort_th." ".$sort_type;
			$appoint_list = $this->appoint_model->getAppointList($where,$length,$start,$order_by);
			foreach($appoint_list as $key => $value) {
				$areas = unserialize($value['areas']);
				$appoint_list[$key]['create_time'] = date("Y-m-d H:i",$value['create_time']);
				$appoint_list[$key]['addr'] = $areas['detail'].$value['address'];
				$appoint_list[$key]['total_price'] = $value['total_price']."元";
				$appoint_list[$key]['coupon_price'] = $value['coupon_price']."元";
				$appoint_list[$key]['real_total'] = $value['real_total']."元";
				if($value['appoint_status'] == 1) {
					$pay_status = 2;	
					$pay_text = "付款";	
					$appoint_list[$key]['pay_now'] = "未付款";
				}else if($value['appoint_status'] == 2) {
					$pay_status = 1;
					$pay_text = "取消付款";
					$appoint_list[$key]['pay_now'] = "已付款";
				}
				if($value['insure_status'] == 1) {
					$insure_status = 2;
					$insure_text = "投保";
					$appoint_list[$key]['insure_now'] = "未投保";
				}else if($value['insure_status'] == 2) {
					$insure_status = 1;
					$insure_text = "取消投保";
					$appoint_list[$key]['insure_now'] = "已投保";
				}
				if($value['sign_status'] == 1) {
					$sign_status = 2;
					$sign_text = "签收";
					$appoint_list[$key]['sign_now'] = "未签收";
				}else if($value['sign_status'] == 2) {
					$sign_status = 1;		
					$sign_text = "未签收";					
					$appoint_list[$key]['sign_now'] = "已签收";
				}
				if($value['gift_status'] == 1) {
					$gift_status = 2;
					$gift_text = "发放";
					$appoint_list[$key]['gift_now'] = "未发放";
				}else if($value['gift_status'] == 2) {
					$gift_status = 1;
					$gift_text = "取消发放";
					$appoint_list[$key]['gift_now'] = "已发放";
				}
				if($value['data_status'] == 1) {
					$data_status = 0;
					$data_text = "冻结";
					$appoint_list[$key]['data_now'] = "未冻结";
				}else if($value['data_status'] == 0) {
					$data_status = 1;
					$data_text = "解冻";
					$appoint_list[$key]['data_now'] = "已冻结";
				}	
				$appoint_list[$key]['status_now'] = $appoint_list[$key]['pay_now']." ".$appoint_list[$key]['insure_now']." ".$appoint_list[$key]['sign_now']." ".$appoint_list[$key]['gift_now']." ".$appoint_list[$key]['data_now'];
				$del_oper = "onclick=\"if(confirm('确实要操作此条记录吗？')) return true;else return false; \"";
				$edit_url = '<a href="/www/appoint/edit_page/'.$value['id'].'"><span class="btn btn-xs btn-warning">编辑</span></a>';			
				$pay_url = '<a href="/www/appoint/appoint_operate?id='.$value['id'].'&appoint_status='.$pay_status.'" '.$del_oper.'><span class="btn btn-xs  btn-success">'.$pay_text.'</span></a>';
				$insure_url = '<a href="/www/appoint/appoint_operate?id='.$value['id'].'&insure_status='.$insure_status.'" '.$del_oper.'><span class="btn btn-xs btn-danger">'.$insure_text.'</span></a>';
				$sign_url = '<a href="/www/appoint/appoint_operate?id='.$value['id'].'&sign_status='.$sign_status.'" '.$del_oper.'><span class="btn btn-xs btn-primary">'.$sign_text.'</span></a>';
				$data_url = '<a href="/www/appoint/appoint_operate?id='.$value['id'].'&data_status='.$data_status.'" '.$del_oper.'><span class="btn btn-xs btn-purple">'.$data_text.'</span></a>';
				$gift_url = '<a href="/www/appoint/appoint_operate?id='.$value['id'].'&gift_status='.$gift_status.'" '.$del_oper.'><span class="btn btn-xs btn-inverse">'.$gift_text.'</span></a>';
				$appoint_list[$key]['operate'] = $edit_url." ".$pay_url." ".$insure_url." ".$sign_url." ".$data_url." ".$gift_url;
			}
			$aaData = $appoint_list;
	
		}
		$output['aaData'] = $aaData;
		$output['sEcho'] = $_GET['sEcho'];
		$output['iTotalDisplayRecords'] =  $this->data['count']; //总共有几条数据
		$output['iTotalRecords'] = $this->data['count']; //总共有几条数据
		echo json_encode($output); //最后把数据以json格式返回
	}
	
	/**
	 * @brief 进入编辑页面
	 * @param Null
	 * @par 2015/10/23 Ver 1.00 Created by Allen
	 */
	public function edit_page() {
		$id = $this->uri->segment(4);
		$where['appoint.id'] = $id;
		$appoint_info = $this->appoint_model->checkAppoint($where);
		$areas = $appoint_info['areas'];
		$areas = unserialize($areas);
		$appoint_info['addr'] = $areas['detail'].$appoint_info['address'];
		$this->data['data'] = $appoint_info;
		$this->load->view('/www/appoint_edit',$this->data);
	}
			
	/**
	 * @brief 保存编辑信息
	 * @param Null
	 * @par 2015/10/23 Ver 1.00 Created by Allen
	 */
	public function edit() {
		$data = $_POST;
		$data['update_time'] = time();
		$where['id'] = $data['id'];
		$edit_result = $this->appoint_model->editAppoint($where,$data);
		$go_url = '/www/appoint/appoint_list';
		if($edit_result) {
			$this->location_href($this->lang->line('edit_success'),$go_url);	
		}else {
			$this->location_href($this->lang->line('edit_false'),$go_url);	
		}
	}
	
	/**
	 * @brief 操作按钮
	 * @param Null
	 * @par 2015/10/22 Ver 1.00 Created by Allen
	 */
	public function appoint_operate() {
		$data = $_GET;
		$where['id'] = $data['id'];
		$operate_result = $this->appoint_model->editAppoint($where,$data);
		$go_url = '/www/appoint/appoint_list';
		if($operate_result) {
			$this->location_href($this->lang->line('operate_success'),$go_url);
		}else {
			$this->location_href($this->lang->line('operate_false'),$go_url);
		}
	}
	
}
?>